﻿using Domain.Entitys;
using Domain.Repositorys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class MCRepositoy : IMCRepositoy
    {
        private readonly MyDbContext myDbContext;

        public MCRepositoy(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public List<HF> HFDFenYe(int a, int b, string? s1, DateTime? s2, DateTime? s3)
        {
            List<HF> list = myDbContext.HF.Where(e => e.CheckStatus == "已复核" && e.HumanFileStatus == "正常").ToList();
            if (s1 != null)
            {
                list = list.Where(e => e.ThirdKindId == s1).ToList();
            }
            if (s2 != null)
            {
                list = list.Where(e => e.RegistTime >= s2 && e.RegistTime <= s3).ToList();
            }
            return list.Skip((a - 1) * b).Take(b).ToList();
        }

        public int HFDCount(string? s1, DateTime? s2, DateTime? s3)
        {
            List<HF> list = myDbContext.HF.Where(e => e.CheckStatus == "已复核" && e.HumanFileStatus == "正常").ToList();
            if (s1 != null)
            {
                list = list.Where(e => e.ThirdKindId == s1).ToList();
            }
            if (s2 != null)
            {
                list = list.Where(e => e.RegistTime >= s2 && e.RegistTime <= s3).ToList();
            }
            return list.Count();
        }

        public async Task<HF> GetHFDById(int id)
        {
            return await myDbContext.HF.FirstOrDefaultAsync(e => e.HufId == id);
        }

        public async Task<int> MCAdd(MC mc)
        {
            await myDbContext.MC.AddAsync(mc);
            return await myDbContext.SaveChangesAsync();
        }

        /*        public int HFDUpdate(int id, string fd, string fn, string sd, string sn, string td, string tn, string kd, string kn, string md, string mn, string salary)
                {
                    HFD hfd = myDbContext.HFDs.FirstOrDefault(e => e.HfdId == id);
                    hfd.FirstKindId = fd;
                    hfd.FirstKindName = fn;
                    hfd.SecondKindId = sd;
                    hfd.SecondKindName = sn;
                    hfd.ThirdKindId = td;
                    hfd.ThirdKindName = tn;
                    hfd.HumanMajorKindId = kd;
                    hfd.HumanMajorKindName = kn;
                    hfd.HumanMajorId = md;
                    hfd.HunmaMajorName = mn;
                    hfd.SalaryStandardId = salary;
                    myDbContext.HFDs.Update(hfd);
                    return myDbContext.SaveChanges();
                }*/

        public List<MC> MCFenYe(int a, int b, string? s1, DateTime? s2, DateTime? s3)
        {
            List<MC> list = myDbContext.MC.Where(e => e.CheckStatus
            == "待复核").ToList();
            if (s1 != null)
            {
                list = list.Where(e => e.ThirdKindId == s1).ToList();
            }
            if (s2 != null)
            {
                list = list.Where(e => e.RegistTime >= s2 && e.RegistTime <= s3).ToList();
            }
            return list.Skip((a - 1) * b).Take(b).ToList();
        }

        public int MCCount(string? s1, DateTime? s2, DateTime? s3)
        {
            List<MC> list = myDbContext.MC.Where(e => e.CheckStatus
            == "待复核").ToList();
            if (s1 != null)
            {
                list = list.Where(e => e.ThirdKindId == s1).ToList();
            }
            if (s2 != null)
            {
                list = list.Where(e => e.RegistTime >= s2 && e.RegistTime <= s3).ToList();
            }
            return list.Count();
        }

        public async Task<MC> GetMCById(int id)
        {
            return await myDbContext.MC.FirstOrDefaultAsync(e => e.MchId == id);
        }

        public int MCUpdate(MC mc)
        {
            myDbContext.MC.Update(mc);
            return myDbContext.SaveChanges();
        }

        public List<MC> MCFenYe2(int a, int b, string? s1, DateTime? s2, DateTime? s3)
        {
            List<MC> list = myDbContext.MC.Where(e => e.CheckStatus == "通过").ToList();
            if (s1 != null)
            {
                list = list.Where(e => e.ThirdKindId == s1).ToList();
            }
            if (s2 != null)
            {
                list = list.Where(e => e.RegistTime >= s2 && e.RegistTime <= s3).ToList();
            }
            return list.Skip((a - 1) * b).Take(b).ToList();
        }

        public int MCCount2(string? s1, DateTime? s2, DateTime? s3)
        {
            List<MC> list = myDbContext.MC.Where(e => e.CheckStatus == "通过").ToList();
            if (s1 != null)
            {
                list = list.Where(e => e.ThirdKindId == s1).ToList();
            }
            if (s2 != null)
            {
                list = list.Where(e => e.RegistTime >= s2 && e.RegistTime <= s3).ToList();
            }
            return list.Count();
        }

        public int HFDUpdate(int id, string fd, string fn, string sd, string sn, string td, string tn, string kd, string kn, string md, string mn, string salary)
        {
            HFD hfd = myDbContext.HFD.FirstOrDefault(e => e.HfdId == id);
            hfd.FirstKindId = fd;
            hfd.FirstKindName = fn;
            hfd.SecondKindId = sd;
            hfd.SecondKindName = sn;
            hfd.ThirdKindId = td;
            hfd.ThirdKindName = tn;
            hfd.HumanMajorKindId = kd;
            hfd.HumanMajorKindName = kn;
            hfd.HumanMajorId = md;
            hfd.HunmaMajorName = mn;
            hfd.SalaryStandardId = salary;
            myDbContext.HFD.Update(hfd);
            return myDbContext.SaveChanges();
        }

        public int HFDUpdate(int id, string fd, string fn, string sd, string sn, string td, string tn, int kd, string kn, int md, string mn, int salary)
        {
            throw new NotImplementedException();
        }
    }
}
